#coding=utf-8
"""
Author:陆孝
Time:2025/9/4 0004 11:04
"""

import openpyxl
from utils.handle_path import *
import os

class Handle_Excel():
    '''封装读取Excel表格的工具类'''

    def __init__(self,filename,sheet_name):
        self.filename = filename
        self.sheet_name = sheet_name

    def open(self):
        '''封装一个打开Excel表格的工具方法'''
        self.wb = openpyxl.load_workbook(self.filename)
        self.sh = self.wb[self.sheet_name]

    def read_data(self):
        '''封装一个读取Excel表格的工具方法'''
        self.open()
        datas = list(self.sh.rows)
        # 列表解析式
        title = [i.value for i in datas[0]]

        cases = []
        for j in datas[1:]:
            values = [k.value for k in j]
            case =dict(zip(title,values))
            cases.append(case)
        return cases # 把所有的用例进行返回

    def write_excel(self,row,col,value=None):
        '''封装一个往Excel表格里面写入测试结果的工具方法'''
        # 先打开表格
        self.open()
        # 往固定的行和列写入value数据
        self.sh.cell(row,col,value)
        # 保存数据
        self.wb.save(self.filename)

# if __name__ == '__main__':
#     path = os.path.join(data_path,'apicase.xlsx')
#     read_excel = Handle_Excel(path,'login')
#     value = read_excel.read_data()
#     read_excel.write_excel(2,8,"通过")